<?php
namespace app\office\controller;

use think\Request;
use app\office\controller\Base;
use app\office\model\Employee as EmployeeModel;

class Employee extends OfficeBase
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    // protected $filter=['is_sth'=>'0','sth_num'='0','static'=>'1'];
    protected $filter=[];
    public function index()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员列表");
      // 此处根据是否有搜索信息来进行筛选
      $order='id';
      $order_method='desc';
      if(!empty(input('request.'))){
        $data=input('request.');
        foreach($data as $k=>$v){
          if(in_array($k,['is_sth','sth_num','static'])&$v!=null){     /* 使用in_array($k,['is_sth','sth_num','static'],是因为paginate传参是get方法,需要筛选变量 */
            $this->filter[$k]=$v;
          }
        }
        $order=$data['order'];
        $order_method=$data['order_method'];
      }
      $filter_msg=$this->filter;
      $filter_msg['order']=$order;
      $filter_msg['order_method']=$order_method;
      // dump($filter_msg);
      $list=EmployeeModel::scope('static')->where($this->filter)->order($order,$order_method)->paginate(5,false,['query' =>$filter_msg]);   /* 这里用的是查询构建器的select()方法;或者可以使用模型的all()方法,$list=EmployeeModel::all(); */
      $countall=EmployeeModel::scope('static')->where($this->filter)->count();
      $this->assign('filter_msg',$filter_msg);
      $this->assign('list',$list);
      $this->assign('count',count($list));
      $this->assign('countall',$countall);
      return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();     
      $this->assign('title',"雇员信息创建");
      // 获取雇员编号
      $code=$this->makeCode('employee');
      $this->assign('code',$code);
      return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
      $employee=new EmployeeModel;
      // 保存图片类文件
      if($file = $request->file('pic')){
        $employee->pic_name=$this->savePic($file);
      }
      if($employee->allowField(true)->validate(true)->save(input('post.'))){
        $this->success('员工'.$employee->code.":".$employee->title.'新增成功','index','',$wait = 1);
      }else{
        return $employee->getError();
      }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员信息");
      $employee=EmployeeModel::get($id);
      $this->assign('employee',$employee);
      return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
      //验证Cookie↓
      $this->checkCookie();
      // 引入navbar
      $this->setNavbar();
      $this->assign('title',"雇员信息编辑");
      $employee=EmployeeModel::get($id)->getData();     /* 后面的getData()是在定义了获取器的情况下来获得原始数据 */
      $employee['create_time']=date('Y-m-d',strtotime($employee['create_time']));
      $employee['update_time']=date('Y-m-d',strtotime($employee['update_time']));
      $this->assign('employee',$employee);
      return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
      // 验证提交的数据
      $employee = EmployeeModel::get($id);
      $data = input('post.');
      $result=$this->validate($data,'Employee');
      if(true !== $result){
        return $result;
      }
      // 保存图片类文件
      if($file = $request->file('pic')){
        $employee->pic_name=$this->savePic($file);
      }
      $employee->title     =input('post.title');
      $employee->password  =input('post.password');
      $employee->address   =input('post.address');
      $employee->detail    =input('post.detail');
      $employee->is_sth    =input('post.is_sth');
      $employee->sth_num   =input('post.sth_num');
      $employee->static    =input('post.static');
      if($employee->allowField(true)->save()){
        $this->success('员工'.$employee->code.":".$employee->title.'修改成功','index',2);
      }else{
        $this->success('数据修改失败,请联系管理员','index');
      }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
      //此处为彻底删除(还应该判断关联关系),入口在edit页面,'删除'实际上是更新,将static=0;
      $employee=EmployeeModel::get($id);
      $result=EmployeeModel::destroy($id);       /* 可以使用delete方法 */
      if($result){
      // $this->success('员工'.$employee->code.":".$employee->title.'删除成功','index');
        $this->redirect('index');
      }else{
        $this->error('员工删除失败,请联系管理员','index');
      }
    }


}
